package cn.new7as.cas5s.ui.login;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;

import androidx.annotation.NonNull;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnHttpListener;

import cn.new7as.cas5s.ui.slideshow.BaseActivity;
import cn.new7as.cas5s.R;
import cn.new7as.cas5s.databinding.ActivityChangepwdBinding;
import cn.new7as.cas5s.ui.easy.netapi.HttpData;
import cn.new7as.cas5s.ui.easy.netapi.ChangePwdApi;


public class ChangePwdActivity extends BaseActivity {


    private ActivityChangepwdBinding binding;
    private boolean isshoweye;
    private boolean isshoweye2;
    private boolean isshoweye3;

    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityChangepwdBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setop("Change Password");

        binding.verifyme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ph = binding.newpwd.getText().toString();
                String old = binding.oldpwd.getText().toString();
                String newpwdconf = binding.newpwdconf.getText().toString();
                if (!ph.equals(newpwdconf)){
                    toa("THe new password and the confirmation password not match");
                    return;
                }
                setnewpwd(getunique(),old, ph);
            }
        });

        binding.show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isshoweye) {
                    binding.oldpwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    binding.show.setBackgroundResource(R.mipmap.eye_close);
                } else {
                    binding.oldpwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    binding.show.setBackgroundResource(R.mipmap.eye_open);
                }

                isshoweye = !isshoweye;
            }
        });
        binding.shownew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isshoweye2) {
                    binding.newpwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    binding.shownew.setBackgroundResource(R.mipmap.eye_close);
                } else {
                    binding.newpwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    binding.shownew.setBackgroundResource(R.mipmap.eye_open);
                }

                isshoweye2 = !isshoweye2;
            }
        });

        binding.showconfi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isshoweye3) {
                    binding.newpwdconf.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    binding.showconfi.setBackgroundResource(R.mipmap.eye_close);
                } else {
                    binding.newpwdconf.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    binding.showconfi.setBackgroundResource(R.mipmap.eye_open);
                }

                isshoweye3 = !isshoweye3;
            }
        });
    }

    void setnewpwd(String pho, String ver, String newp) {
        EasyHttp.post(this)
                .api(new ChangePwdApi(pho, ver,newp)).request(new OnHttpListener<HttpData<Object>>() {
                    @Override
                    public void onHttpSuccess(@NonNull HttpData<Object> result) {

                        finish();

                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);

                    }
                });
    }

}